package appController

import (
	"CMETO/models"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// app部分的接口
type NoticeController struct{}

// @Summary 通知查询
// @Description 查询通知
// @Tags 通知管理
// @Accept json
// @Produce json
// @Param page query string false "页码"
// @Param pageSize query string false "尺寸"
// @Param name query string false "通知名"
// @Success 200 {object} models.ResultCommon[[]models.Notice] "查询成功"
// @Success 400 {object} models.ErrorResult "查询失败"
// @Router /app/notice [get]
// @Security JWT
func (notice NoticeController) QueryNotices(ctx *gin.Context) {
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
	nameStr := ctx.DefaultQuery("name", "")
	name := "%" + nameStr + "%"
	noticeList := []models.NoticeFormat{}
	models.DB.Model(&models.Notice{}).
		Select("notices.*,users.username as create_user").
		Joins("LEFT JOIN users ON notices.create_user_id = users.id").
		Where("notices.is_delete!=1 AND notices.title like ?", name).Limit(pageSize).Offset(pageSize * (page - 1)).
		Scan(&noticeList)
	var total int64
	models.DB.Table("notices").Where("is_delete!=1 AND title like ?", name).Count(&total)
	result := &models.ResultByPage[models.NoticeFormat]{
		Msg:      "ok",
		Code:     "200",
		Total:    total,
		Page:     page,
		PageSize: pageSize,
		Result:   noticeList,
	}
	ctx.JSON(http.StatusOK, result)
}
